<?php

namespace common\models\site;

use Yii;
use common\helpers\Url;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%map_fence}}".
 *
 * @property int $id
 * @property string $name 围栏标识
 * @property string $title 围栏名称
 * @property string $description 描述信息
 * @property string $thumb 缩略图
 * @property string $color 颜色值
 * @property int $map_id 地图ID
 * @property string $map_data 地图所需参数值
 * @property string $scope 应用范围
 * @property string $mark 备注
 * @property int $status 状态
 * @property int $created_at 创建时间
 * @property int $updated_at 更新时间
 * @property int $deleted_at 删除时间
 */
class MapFence extends \common\db\ActiveRecord {

    /**
     * {@inheritdoc}
     */
    public static function tableName() {
        return '{{%map_fence}}';
    }

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['title'], 'required'],
            [['map_id', 'status', 'created_at', 'updated_at', 'deleted_at'], 'integer'],
            [['map_data'], 'string'],
            [['name', 'scope'], 'string', 'max' => 30],
            [['title'], 'string', 'max' => 100],
            [['thumb', 'mark', 'description'], 'string', 'max' => 255],
            [['color'], 'string', 'max' => 7],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function attributeLabels() {
        return [
            'id' => 'ID',
            'name' => '围栏标识',
            'title' => '围栏名称',
            'description' => '描述信息',
            'thumb' => '缩略图',
            'color' => '颜色值',
            'map_id' => '地图ID',
            'map_data' => '地图所需参数值',
            'scope' => '应用范围标识',
            'mark' => '备注',
            'status' => '状态',
            'created_at' => '创建时间',
            'updated_at' => '更新时间',
            'deleted_at' => '删除时间',
        ];
    }

    /**
     * 根据应用范围标识查询内容
     * @param type $scope
     */
    public static function findAllByScope($scope) {
        return static::find()->active()->andWhere([
            'scope' => $scope
        ])->all();
    }

    /**
     * 根据应用范围标识查询内容
     * @param type $scope
     */
    public static function findOneByScope($scope) {
        return static::find()->active()->andWhere([
            'scope' => $scope
        ])->one();
    }

    /**
     * 根据应用范围标识查询内容
     * @param type $name
     */
    public static function findAllByName($name) {
        return static::find()->active()->andWhere([
            'name' => $name
        ])->all();
    }

    /**
     * 根据应用范围标识查询内容
     * @param type $name
     */
    public static function findOneByName($name) {
        return static::find()->active()->andWhere([
            'name' => $name
        ])->one();
    }

    /**
     * 获取weblink路由跳转
     */
    public function getWeblink() {
        $data = [];
        if (!empty($this->map_data)) {
            $data = unserialize($this->map_data);
        }
        return Url::weblink($this->map_id, $data);
    }

    /**
     * {@inheritdoc}
     */
    public function fields() {
        return [
            'id',
            'title',
            'description',
            'thumb' => function() {
                return Url::attach($this->thumb);
            },
            'weblink',
        ];
    }

}
